package cn.peach.pet.controller;

import cn.peach.basic.exception.MyException;
import cn.peach.basic.util.AjaxResult;
import cn.peach.basic.util.LoginContextUtil;
import cn.peach.basic.util.PageList;
import cn.peach.pet.domain.Pet;
import cn.peach.pet.domain.SearchMasterMsg;
import cn.peach.pet.query.SearchMasterMsgQuery;
import cn.peach.pet.service.ISearchMasterMsgService;
import cn.peach.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }

    /**
     * 发布寻主
     * @param smm
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.publish(smm,logininfo.getId());
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统炸了");
        }
    }

    /**
     * 待处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.pending(query, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 已处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return seachMasterMsgService.processed(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 添加修改
     * @param query
     * @param request
     * @return
     */
   /* @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.handle(pet,logininfo.getId());
            if (logininfo.getId() != null){
                seachMasterMsgService.update(pet);
            }else {
                seachMasterMsgService.insert(pet);
            }
            return AjaxResult.me();
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统进水了0^0");
        }
    }*/

    /**
     * 删除
     * @param pet
     * @param request
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            seachMasterMsgService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统进水了0^0");
        }
    }

    @PostMapping("/dels")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            seachMasterMsgService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统进水了0^0");
        }
    }
}
